package com.example.ymm.modules1.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.sys.SysRole;
import com.example.ymm.modules1.entity.my.sys.SysRoleMenu;
import com.example.ymm.modules1.mapper.my.sys.SysRoleMapper;
import com.example.ymm.modules1.query.sys.SysRoleMenuQueryCriteria;
import com.example.ymm.modules1.query.sys.SysRoleQueryCriteria;
import com.example.ymm.modules1.service.sys.SysRoleMenuService;
import com.example.ymm.modules1.service.sys.SysRoleService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
//@Transactional
@Slf4j
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    SysRoleMenuService sysRoleMenuService;

    @Override
    public TableList<SysRole> list(SysRoleQueryCriteria criteria, YmPageDto pageDto) {
        IPage<SysRole> page = new Page<SysRole>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(SysRole.class, criteria);
        predicate.orderByAsc("sort");
        baseMapper.selectPage(page, predicate);
        TableList<SysRole> tableList=new TableList(page);
        return tableList;
    }

    public List<SysRole> queryAll(SysRoleQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(SysRole.class, criteria);
        predicate.orderByAsc("sort");
        List<SysRole> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public SysRole detailFn(SysRole role) {
        SysRole role1 = getById(role);
        SysRoleMenuQueryCriteria criteria=new SysRoleMenuQueryCriteria();
        criteria.setRoleValue(role.getRoleValue());
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.queryAll(criteria);
        List<String> ids=new ArrayList<>();
        sysRoleMenus.forEach(r->{
            ids.add(r.getMenuId());
        });
        role1.setMenuIds(String.join(",",ids));
        return role1;
    }

    @Override
    public void saveFn(SysRole role) {
        //查找旧值
        SysRole oldRole=null;
        if(UtilValidate.isNotEmpty(role.getId())){
            oldRole=getById(role.getId());
            if(UtilValidate.isNotEmpty(role.getMenuIds())){
                sysRoleMenuService.remove(new QueryWrapper<SysRoleMenu>().eq("role_id", role.getId()));

                sysRoleMenuService.remove(new QueryWrapper<SysRoleMenu>().eq("role_value", oldRole.getRoleValue()));
            }
            sysRoleMenuService.removeByRoleValue(role.getRoleValue());
        }
        saveOrUpdate(role);
        if(UtilValidate.isNotEmpty(role.getMenuIds())){
            String[] ids = role.getMenuIds().split(",");
            List<SysRoleMenu> arr=new ArrayList<>();
            for(String menuId: ids){
                SysRoleMenu roleMenu=new SysRoleMenu();
                roleMenu.setRoleValue(role.getRoleValue());
                roleMenu.setMenuId(menuId);
                roleMenu.setRoleId(role.getId());
                arr.add(roleMenu);
            }
            sysRoleMenuService.saveOrUpdateBatch(arr);
        }
    }
}
